from time import sleep

import yaml
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from uitls.file_util import FileUtil


project_name = FileUtil.get_project_path()


class GetCookies:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)
        self.action = ActionChains(self.driver)

    def get_cookie(self):
        self.driver.get("https://cj8m5s4d8c.feishu.cn/calendar/week")
        # 点击切换登录方式
        self.driver.find_element(By.CSS_SELECTOR, '.switch-icon').click()
        # 输入手机号码
        self.driver.find_element(By.NAME, 'mobile_input').send_keys('17770026282')
        # 勾选隐私条款
        self.driver.find_element(By.CSS_SELECTOR,'div.base-check-box-container .base-check-box').click()
        # 点击下一步
        self.driver.find_element(By.XPATH, '//div[@data-test="login-phone-next-btn"]').click()
        # 点击密码登录
        ele: WebElement = WebDriverWait(self.driver,10,2).until(expected_conditions.element_to_be_clickable((By.XPATH, '//span[contains(text(),"密码登录")]')))
        ele.click()
        # self.action.move_to_element(ele).click().perform()
        # self.driver.find_element(By.XPATH, '//span[contains(text(),"密码登录")]').click()
        # 输入密码
        self.driver.find_element(By.XPATH, '//input[@type="password"]').send_keys('doubi0606..')
        # 点击下一步
        self.driver.find_element(By.XPATH, '//div[@data-test="login-pwd-next-btn"]').click()
        # 获取cookie
        cookies = self.driver.get_cookies()
        # 将cookie写入yaml中
        with open(f'{project_name}/conf/cookies.yaml', 'w', encoding='utf-8') as f:
            yaml.safe_dump(cookies, f)


if __name__ == '__main__':
    GetCookies().get_cookie()